//
//  HMStatusRetweetView.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/24.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

/// 被转发微博的View
class HMStatusRetweetView: UIView {
    
    /// 被转发微博模型,有可能有,也有可能没有
    var retweetStatus: HMStatus? {
        didSet {
            // 拿到被转发微博模型,设置内容
            if retweetStatus == nil {
                retweetContentLabel.text = "没有被转发微博!!!"
            } else {
//                let name = retweetStatus?.user?.screen_name ?? "没有名称"
//                let text = retweetStatus?.text ?? "没有微博内容"
//                let content = "@ \(name): \(text)"
                
                // 让被转发模型提供拼接好的文字
                
                retweetContentLabel.text = retweetStatus?.retweetedText
            }
            
            // 设置配图图片, 有可能没有被转发微博
            retweetPictureView.imageURLs = retweetStatus?.storePicutureURLs
            
            // 判断是否有图片,有图片配图顶部到文字底部间距8,没有图片,配图的顶部到文字底部0
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                var margin: CGFloat = 0
                if retweetStatus?.storePicutureURLs != nil && retweetStatus?.storePicutureURLs?.count > 0 {
                    margin = CellMargin
                }
                
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        backgroundColor = UIColor.brownColor()
        
        setupUI()
    }
    
    /// 设置UI界面
    private func setupUI() {
        // 添加控件
        self.addSubview(retweetContentLabel)
        self.addSubview(retweetPictureView)
        
        // 添加约束
        // 被转发微博文本内容
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(CellMargin)
        }
        
        // 被转发微博图片
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CellMargin)
            make.size.equalTo(CGSize(width: 300, height: 300))
        }
        
        // 底部约束,被转发view的底部和配图的底部重合
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(CellMargin)
        }
    }

    // MARK: - 懒加载
    /// 被转发的label
    private lazy var retweetContentLabel: FFLabel = {
        let label = FFLabel(textColor: UIColor.darkGrayColor(), fontSize: CellFontSize)
        
        // 设置换行宽度
        label.preferredMaxLayoutWidth = UIScreen.width() - 2 * CellMargin
        label.numberOfLines = 0
        label.labelDelegate = self
        
        return label
    }()
    
    /// 转发微博配图
    private lazy var retweetPictureView: HMStatusPictureView = HMStatusPictureView()
}

extension HMStatusRetweetView: FFLabelDelegate {
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        print("点中了2: \(text)")
    }
}
